#include <functional>
#include <memory>
#include <thread>
#include "rclcpp/rclcpp.hpp"

class TestGetParam : public rclcpp::Node
{
private:

    // 事件触发机制句柄
    std::shared_ptr<rclcpp::ParameterEventHandler> param_subscriber_;
    std::shared_ptr<rclcpp::ParameterCallbackHandle> cb_handle_;
    std::vector<double> test_param;

    rclcpp::TimerBase::SharedPtr timer_;

public:
    TestGetParam();
    ~TestGetParam();

    void get_param()
    {
        test_param = this->get_parameter("test.param").as_double_array();

        RCLCPP_INFO(this->get_logger(), "test_param is %lf  %lf", test_param[0], test_param[1]);
    }

};

TestGetParam::TestGetParam() : Node("test_get_param")
{
    this->declare_parameter("test.param", std::vector<double>{2.0, 2.0});

    param_subscriber_ = std::make_shared<rclcpp::ParameterEventHandler>(this);
    // Lambda表达式
    auto fre_cb = [this](const rclcpp::Parameter &p)
    {
        this->test_param = p.as_double_array();
        RCLCPP_INFO(this->get_logger(), "test_param is %lf  %lf", test_param[0], test_param[1]);
    };
    // 注册参数变更回调
    cb_handle_ = param_subscriber_->add_parameter_callback("test.param", fre_cb, "test_set_param");

    this->timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&TestGetParam::get_param, this));
}

TestGetParam::~TestGetParam()
{
}


int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);

    auto node = std::make_shared<TestGetParam>();

    rclcpp::spin(node);

    rclcpp::shutdown();
    return 0;
}